/*
 * Worker.h
 *
 *  Created on: 13/06/2009
 *      Author: banouz
 */

#ifndef WORKER_H_
#define WORKER_H_

#include "OSThread.h"
#include "MyMessage.h"
#include "msgComperator.h"
#include "mc.h"
#include <queue>


//#include <iostream>
//#include <string>
//#include <iomanip>
//#include <fstream>
//#include <sstream>
//#include <vector>
//#include <map>
//#include <stdio.h>
//#include <stdlib.h>



//#include <sstream>



using namespace std;

class Worker : public OSThread
{
private:
    pthread_cond_t m_outOfMess;
    pthread_mutex_t m_quantaLock2;

  //  priority_queue<MyMessage,vector<MyMessage> ,msgComperator> _outMBox;
   // priority_queue<MyMessage,vector<MyMessage> ,msgComperator> _inMBox;

    priority_queue<MyMessage*,vector<MyMessage*> ,mc> _outMBox;
    priority_queue<MyMessage*,vector<MyMessage*> ,mc> _inMBox;


    int _newMessFlag;
    bool _isKilled;
    string _myID;
    int _realID;
    bool _exit;


    priority_queue<MyMessage*,vector<MyMessage*> ,mc> *_inMailerMBoxRef;
    priority_queue<MyMessage*,vector<MyMessage*> ,mc> *_outMailerMBoxRef;
    map<string,vector<string> > *_graph;
    map<string,vector<string> > _rTable;
    map<string,map<string,vector<string> > > *_RTi;
    map<string,vector<string> > *_RTiArr;

public:
    Worker();
    virtual ~Worker();
    Worker(map<string,vector<string> >& tMap, string ID);
    void run();
    bool studentSend(void *args);
    bool studentRecv(void **args);
    bool checkUpdt(void *args);
    void addMess();
    void addToMail();
    priority_queue<MyMessage*,vector<MyMessage*> ,mc> *getinMailerMBoxRef() const
    {
        return _inMailerMBoxRef;
    }

    void setInMailerMBoxRef(priority_queue<MyMessage*,vector<MyMessage*> ,mc> & mailerMBoxRef)
    {
        _inMailerMBoxRef = &mailerMBoxRef;
    }

    priority_queue<MyMessage*,vector<MyMessage*> ,mc> *getoutMailerMBoxRef() const
    {
        return _outMailerMBoxRef;
    }

    void setOutMailerMBoxRef(priority_queue<MyMessage*,vector<MyMessage*> ,mc> & mailerMBoxRef)
    {
        _outMailerMBoxRef = &mailerMBoxRef;
    }

    void setGraph(map<string,vector<string> > & graph)
    {
        _graph = &graph;
        _RTiArr = new map<string,vector<string> >[(int)((((_graph->size()))))];
    }

    int getrealID() const
    {
        return _realID;
    }

    void setrealID(int _myID)
    {
        cout << "cc  " << _realID << endl;
        this->_realID = _myID;
    }

    string getMyID() const
    {
        return _myID;
    }

    void setMyID(string _myID)
    {
        this->_myID = _myID;
    }

    map<string,vector<string> > getTable() const
    {
        return _rTable;
    }

    void setTable(map<string,vector<string> > _rTable)
    {
        this->_rTable = _rTable;
    }

    bool isMailBoxEmpty();
    pthread_cond_t getOutOfMess() const
    {
        return m_outOfMess;
    }

    void wakeOutOfMess(void);
    void setOutOfMess(pthread_cond_t m_outOfMess)
    {
        this->m_outOfMess = m_outOfMess;
    }

    pthread_mutex_t getQuantaLock2() const
    {
        return m_quantaLock2;
    }

    void setQuantaLock2(pthread_mutex_t m_quantaLock2)
    {
        this->m_quantaLock2 = m_quantaLock2;
    }

    bool getIsKilled() const
    {
        return _isKilled;
    }

    void setIsKilled(bool _isKilled)
    {
        this->_isKilled = _isKilled;
    }

    void setNotKilled()
    {
        this->_isKilled = false;
    }

    priority_queue<MyMessage*,vector<MyMessage*> ,mc> getOutMBox() const
    {
        return _outMBox;
    }

    void setOutMBox(priority_queue<MyMessage*,vector<MyMessage*> ,mc> _outMBox)
    {
        this->_outMBox = _outMBox;
    }

    priority_queue<MyMessage*,vector<MyMessage*> ,mc> getInMBox()
    {
        return _inMBox;
    }

    void setInMBox(priority_queue<MyMessage*,vector<MyMessage*> ,mc> _inMBox)
    {
        this->_inMBox = _inMBox;
    }

    void pushIntoInBox(MyMessage *mess);
    int getNeededQuanta();
    priority_queue<MyMessage*,vector<MyMessage*> ,mc> getOutMBox_B() const
    {
        return _outMBox;
    }

    void setOutMBox_B(priority_queue<MyMessage*,vector<MyMessage*> ,mc> _outMBox)
    {
        this->_outMBox = _outMBox;
    }

    priority_queue<MyMessage*,vector<MyMessage*> ,mc> getInMBox_B() const
    {
        return _inMBox;
    }

    void setInMBox_B(priority_queue<MyMessage*,vector<MyMessage*> ,mc> _inMBox)
    {
        this->_inMBox = _inMBox;
    }

};
//Eran
//Eran



#endif  WORKER_H_
